import axios from "axios";
import config from "./config";
//定义函数完成axios网络请求
function startNetWork(config){
    //开始网络请求
    return axios({
        method:'GET',
        ...config
    }) 
} 

//定义新对象
const network = {};
for(let key in config){
    Object.defineProperty(network, key, {
        async get(){
            if(localStorage.getItem(key)){
                return JSON.parse(localStorage.getItem(key));
            }
            //开始网络请求
            let datas = await startNetWork(config[key].request());
            //确定是否需要数据缓存
            if(!config[key].uncache){
                localStorage.setItem(key, JSON.stringify(datas));
            } 
            return datas; 
        }
    });
}

export default network;
